/*
 * FNISVGEditor - JavaScript - based editor for SVG graphics 
 * Copyright (C) 2008 - 2009 Antti Leppä / Foyt
 * http://www.foyt.fi
 * 
 * License: 
 * 
 * Licensed under GNU Lesser General Public License Version 2.1 or later (the "LGPL") 
 * http://www.gnu.org/licenses/lgpl.html
 */
FNISVGEditorGridPlugin = Class.create(FNISVGEditorPlugin, {
  initialize: function($super, editorInstance, pluginURL) {
    $super(editorInstance, pluginURL);    
	
	var button = new FNIToggleButton({});	
	button.domNode.addClassName('fniSVGEditor_GridPlugin_Button');
	button.addListener('buttonUp', this, this._onButtonUp);
	button.addListener('buttonDown', this, this._onButtonDown);
  button.setTooltip(SVGLocale.getText("gridPlugin_buttonTooltip"));
  editorInstance.registerToolbarItem('grid', button);
	
	this._gridCellWidth = 150;
	this._gridCellHeight = 150;
	this._gridMode = FNISVGEditorGridPlugin.MODE_NORMAL;
	this._gridElements = new Array();
  },  
  getName: function () {
    return "grid";
  },
  _onButtonUp: function () {
  	this._removeGrid();
  },
  _onButtonDown: function () {
  	this._createGrid();
  },
  _createGrid: function () {
  	var viewBox = this.getEditorInstance().getViewPort().getViewBox();
	var svgDocument = this.getEditorInstance().getSVGDocument();

	var x1 = ((Math.round(viewBox.x / this._gridCellWidth) - 1) * this._gridCellWidth);
	var y1 = ((Math.round(viewBox.y / this._gridCellHeight) - 1) * this._gridCellHeight);
	var x2 = ((Math.round((viewBox.width - viewBox.x) / this._gridCellWidth) + 1) * this._gridCellWidth);
	var y2 = ((Math.round((viewBox.height - viewBox.y) / this._gridCellHeight) + 1) * this._gridCellHeight);
	
	switch (this._gridMode) {
	  case FNISVGEditorGridPlugin.MODE_NORMAL:
		for (var x = x1; x <= x2; x += this._gridCellWidth) {
		  var line = svgDocument.createElement("line");  
		  line.style.position = 'absolute';
		  line.setAttributeNS(null, 'stroke-width', '1px');
		  line.setAttributeNS(null, 'stroke', 'black');
		  line.setAttributeNS(null, 'x1', x + 'px');
		  line.setAttributeNS(null, 'y1', y1 + 'px');
		  line.setAttributeNS(null, 'x2', x + 'px');
		  line.setAttributeNS(null, 'y2', y2 + 'px');          
		  svgDocument.appendTemporaryElement(line);
		  this._gridElements.push(line);
		}
		  
		for (var y = y1; y <= y2; y += this._gridCellHeight) {
		  var line = svgDocument.createElement("line");   
		  line.style.position = 'absolute';
		  line.setAttributeNS(null, 'stroke-width', '1px');
		  line.setAttributeNS(null, 'stroke', 'black');
		  line.setAttributeNS(null, 'x1', x1 + 'px');
		  line.setAttributeNS(null, 'y1', y + 'px');
		  line.setAttributeNS(null, 'x2', x2 + 'px');
		  line.setAttributeNS(null, 'y2', y + 'px');  
		  svgDocument.appendTemporaryElement(line);
		  this._gridElements.push(line);
		}   
	  break;
	  case FNISVGEditorGridPlugin.MODE_HEXGRID:	  	
		var corners = new Object();
		var controller = getSVGElementControllerVault().getElementController('path');
		
		for (var y = y1; y <= (y2 * 2); y += (this._gridCellHeight)) {
		  var path = svgDocument.createElement("path");
		  controller.appendSegment(path, new fi.foyt.svg.svgdom.FNISVGEditorPathElementSegment('M', true, [{x: 0, y: this._gridCellHeight / 2}]));
		  var cy = y;
		  for (var x = x1; x <= x2; x += this._gridCellWidth) {
		    var cx1 = x + (this._gridCellHeight * 0.75);
		    var cy1 = cy + (this._gridCellHeight * 0.5);
		    var cx2 = x + this._gridCellHeight;
		    var cy2 = cy;		  
		    cy -= this._gridCellHeight / 2;  	 	  
		    controller.appendSegment(path, new fi.foyt.svg.svgdom.FNISVGEditorPathElementSegment('L', true, [{x: cx1, y: cy1}]));
		    controller.appendSegment(path, new fi.foyt.svg.svgdom.FNISVGEditorPathElementSegment('L', true, [{x: cx2, y: cy2}]));
	      }
		
		  svgDocument.appendTemporaryElement(path);
		  controller.updatePath(path);
		  path.setAttribute('style', 'stroke: black; stroke-width: 1px; fill:none');
		  this._gridElements.push(path); 	   
       }
	   
	   for (var x = x1; x <= (x2 * 2); x += (this._gridCellWidth * 2)) {
		  var path = svgDocument.createElement("path");
		  var cx = x;
		  for (var y = y1; y <= y2; y += this._gridCellHeight) {
		    var cx1 = cx + (this._gridCellWidth * 0.75);
		    var cy1 = y - (this._gridCellHeight * 0.5);
		    var cx2 = cx + this._gridCellWidth;
		    var cy2 = y;		  	  
		    controller.appendSegment(path, new fi.foyt.svg.svgdom.FNISVGEditorPathElementSegment('M', true, [{x: cx1, y: cy1}]));
		    controller.appendSegment(path, new fi.foyt.svg.svgdom.FNISVGEditorPathElementSegment('L', true, [{x: cx2, y: cy2}]));
			
			controller.appendSegment(path, new fi.foyt.svg.svgdom.FNISVGEditorPathElementSegment('M', true, [{x: cx2 + (this._gridCellWidth * 0.75), y: cy2}]));
			controller.appendSegment(path, new fi.foyt.svg.svgdom.FNISVGEditorPathElementSegment('L', true, [{x: cx2 + this._gridCellWidth, y: cy2 + (this._gridCellHeight * 0.5)}]));
	      }
		
		  svgDocument.appendTemporaryElement(path);
		  controller.updatePath(path);
		  path.setAttribute('style', 'stroke: black; stroke-width: 1px; fill:none');
		  this._gridElements.push(path); 	   
       }
		/*
		
		// path2.setAttribute('style', 'stroke: black; stroke-width: 1px; fill:none');		
		controller.updatePath(path1);
				
		
		svgDocument.appendTemporaryElement(path2);
		this._gridElements.push(path2);		
		
		
		*/
		/*
		for (var x = x1; x <= x2; x += this._gridCellWidth) {
		  for (var y = y1; y <= y2; y += this._gridCellHeight) {
		    corners[0] = [x1, ];
			corners[1] = [];
			corners[2] = [];
			corners[3] = [];
			corners[4] = [];
			corners[5] = [];
			corners[6] = [];
			corners[7] = []; 
		  }
		}
	   
		 
		
		
		<path
       d="M 0,50 L 25,0 L 75,0 L 100,-50"
       style="fill:none;stroke:#f3ff00;stroke-width:1px"
       id="path3420" />
    <path
       d="M 25,100 L 75,100 L 100,50"
       style="fill:none;stroke:#f30000;stroke-width:1px"
       id="path3422" />
    <path
       d="M 0,50 L 25,100"
       style="fill:none;stroke:#00ffff;stroke-width:1px"
       id="path3424" />
    <path
       d="M 75,0 L 100,50"
       style="fill:none;stroke:#00f305;stroke-width:1px"
       id="path3426" />*/
	  break;
	}
  },
  _removeGrid: function () {
  	var svgDocument = this.getEditorInstance().getSVGDocument();
	
	while (this._gridElements.length > 0)
	  svgDocument.removeTemporaryElement(this._gridElements.pop());
  }
});

Object.extend(FNISVGEditorGridPlugin, {
  MODE_NORMAL: 'normal',
  MODE_HEXGRID: 'hexgrid'
}); 

document.fire("svgEditorPluginLoaded:grid", { pluginClass: FNISVGEditorGridPlugin});
